﻿using System;
using System.Data;
using System.Windows.Forms;
using SGLLv2.UI.Properties;

namespace SGLLv2.UI
{
    public partial class FormSettings : Template.FormTemplate, IMethods
    {
        // Declare Global Variables
        private MDIParent mdiParent;

        // Class constructor
        public FormSettings()
        {
            InitializeComponent();
        }
        
        // Form
        private void FormSettings_Load(object sender, EventArgs e)
        {
            // Declare Local Variables
            mdiParent = (MDIParent)(ParentForm);
            if (mdiParent == null) return;

            try
            {
                // Start process
                mdiParent.Begin();

                // Load email and password.
                DataTable dataTable = settingS_LASTESTTableAdapter.GetData();

                // Check if data table has data
                if (!dataTable.Rows.Count.Equals(0))
                {
                    textBoxEmail.Text = Utilities.Decrypt(dataTable.Rows[0][0].ToString());
                    textBoxPassword.Text = Utilities.Decrypt(dataTable.Rows[0][1].ToString());
                }

                // Fill out each dataset.
                tYPES_OF_SAMPLES_UNFILTEREDTableAdapter.Fill(dataSetSGLL.TYPES_OF_SAMPLES_UNFILTERED);

                // Prevent from calling future times the Settings Form
                mdiParent.toolStripMenuItemSettings.Enabled = mdiParent.toolStripButtonSettings.Enabled = false;

                // Hookup event
                dataGridView.RowValidating += dataGridView_RowValidating;

                
            }
            catch (Exception exception)
            {
                // Report Error
                mdiParent.SetMessage(MDIParent.MessageType.Error, exception.Message);
                Utilities.ExceptionManager(exception);
            }
            finally
            {
                // End process
                mdiParent.End();
            }
        }
        private void dataGridView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            dataGridView.ClearSelection();
        }

        // Data Grid View
        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            // Update the Updated Date.
            dataGridView[uPDATEDDataGridViewTextBoxColumn.Index, e.RowIndex].Value = DateTime.Now;
        }
        private void dataGridView_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            try
            {
                // Validations
                if (dataGridView[dESCRIPTIONDataGridViewTextBoxColumn.Index, e.RowIndex].Value == null) return;
                if (string.IsNullOrWhiteSpace(dataGridView[dESCRIPTIONDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString())) throw new ArgumentNullException(Resources.MessageWarningSampleDescriptionRequied);
                if (queriesTableAdapter.IsSampleDuplicate(Convert.ToByte(dataGridView[iDDataGridViewTextBoxColumn.Index, e.RowIndex].Value), dataGridView[dESCRIPTIONDataGridViewTextBoxColumn.Index, e.RowIndex].Value.ToString()).Equals(1)) throw new DuplicateNameException(Resources.MessageWarningSampleDuplicate);
            }
            catch (DuplicateNameException exception)
            {
                // Report Error
                mdiParent.SetMessage(MDIParent.MessageType.Warning, exception.Message);

                // Cancel the validating process
                e.Cancel = true;

                // Disable save button
                mdiParent.toolStripMenuItemSave.Enabled = mdiParent.toolStripButtonSave.Enabled = false;

                // Set focus
                dataGridView.BeginEdit(true);
            }
            catch (ArgumentNullException exception)
            {
                // Report Error
                mdiParent.SetMessage(MDIParent.MessageType.Warning, exception.Message);

                // Cancel the validating process
                e.Cancel = true;

                // Disable save button
                mdiParent.toolStripMenuItemSave.Enabled = mdiParent.toolStripButtonSave.Enabled = false;

                // Set focus
                dataGridView.BeginEdit(true);
            }

            catch (Exception exception)
            {
                // Report Error
                mdiParent.SetMessage(MDIParent.MessageType.Error, exception.Message);

                // Cancel the validating process
                e.Cancel = true;
            }
            finally
            {
                // End
                mdiParent.End();
            }
            
        }
        private void dataGridView_RowValidated(object sender, DataGridViewCellEventArgs e)
        {
            // Enable save button
            mdiParent.toolStripMenuItemSave.Enabled = mdiParent.toolStripButtonSave.Enabled = true;

            // Clear any messages on the status bar.
            mdiParent.SetMessage();
        }

        // Interface
        public void New()
        {
            // Add the new row.
            tYPES_OF_SAMPLESBindingSource.AddNew();

            // Fill out all the fields.
            if (dataGridView.CurrentRow != null)
            {
                dataGridView[iDDataGridViewTextBoxColumn.Index, dataGridView.CurrentRow.Index].Value = queriesTableAdapter.GetNextIDTypeOfSamples();
                dataGridView[iSACTIVEDataGridViewCheckBoxColumn.Index, dataGridView.CurrentRow.Index].Value = true;
                dataGridView[iNSERTEDDataGridViewTextBoxColumn.Index, dataGridView.CurrentRow.Index].Value = DateTime.Now;
                dataGridView[uPDATEDDataGridViewTextBoxColumn.Index, dataGridView.CurrentRow.Index].Value = DateTime.Now;

                // Set focus on new cell.
                dataGridView.CurrentCell = dataGridView[dESCRIPTIONDataGridViewTextBoxColumn.Index, dataGridView.CurrentRow.Index];
            }
            dataGridView.BeginEdit(false);
        }
        public void Save()
        {
            try
            {
                // Begin
                mdiParent.Begin();

                // Declare Local Variables

                // Coding
                if (tabControl.SelectedIndex.Equals(tabPageEmail.TabIndex))
                {
                    // Save and let the User Know what's going on.
                    if (queriesTableAdapter.InsertSettings(Utilities.Encrypt(textBoxEmail.Text.Trim()), Utilities.Encrypt(textBoxPassword.Text), Environment.MachineName, Environment.UserName).Equals(1))
                        mdiParent.SetMessage(MDIParent.MessageType.Successful, Resources.MessageSuccessfulSaveEmailInfo);
                    else
                        throw new Exception(Resources.MessageErrorSaveEmailInfo);
                }
                else
                {
                    // Applies pending changes to the underlying data source.
                    tYPES_OF_SAMPLESBindingSource.EndEdit();

                    // Let the User Know what's going on.
                    mdiParent.SetMessage(MDIParent.MessageType.Successful, string.Format(Resources.MessageSuccessfulSaveTyeOfSample, tYPES_OF_SAMPLES_UNFILTEREDTableAdapter.Update(dataSetSGLL.TYPES_OF_SAMPLES_UNFILTERED)));
                }
            }
            catch (Exception exception)
            {
                // Report Error
                Utilities.ExceptionManager(exception);
            }
            finally
            {
                // End
                mdiParent.End();
            }
        }
        public void Print()
        {
            throw new NotImplementedException();
        }
        public void SetControlBox()
        {
            // Toolbar button
            mdiParent.toolStripMenuItemNew.Enabled = mdiParent.toolStripButtonNew.Enabled = true;
            
            // Hooking up the binding source.
            mdiParent.toolStripMenu.BindingSource = tYPES_OF_SAMPLESBindingSource;
        }
        public void ClearFilter()
        {
        }
        public void ClearForm()
        {
        }
        public void ChangeFormView(Global.FormViewModes mode)
        {

        }
        public void EnablePrintButton()
        {

        }
        public void Export()
        {
        }
    }
}